﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace PageAdmin.Utils.ORM
{
    /// <summary>
    /// 参数化构造类
    /// </summary>
    public class DbParameterInfo
    {
        /// <summary>
        /// 参数名
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        ///  参数值
        /// </summary>
        public Object Value { get; set; }

        /// <summary>
        ///长度，只对文本类型有效
        /// </summary>
        public int Size { get; set; } =-1;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="size"></param>
        public DbParameterInfo(string key, object value, int size=-1)
        {
            Key = key;
            Value = value;
            Size = size;
        }

    }

    /// <summary>
    /// 扩展的键值对集合。
    /// </summary>
    public class DbKeyValueList : IEnumerator
    {
        int _Position = -1;

        /// <summary>
        /// 键值对集合
        /// </summary>
        private List<DbParameterInfo> _DbParameterInfoList;

        /// <summary>
        /// 
        /// </summary>
        public DbKeyValueList()
        {
            _DbParameterInfoList = new List<DbParameterInfo>();
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="size"></param>
        public void Add(string key, object value, int size =-1)
        {
            Func<DbParameterInfo, bool> func = c => c.Key.Equals(key, StringComparison.OrdinalIgnoreCase);
            DbParameterInfo item = _DbParameterInfoList.Where(func).FirstOrDefault();
            if (item != null)
            {
                _DbParameterInfoList.Remove(item);//key存在则先移出
            }
            _DbParameterInfoList.Add(new DbParameterInfo(key, value, size));
        }

        /// <summary>
        /// 获取集合总数
        /// </summary>
        public int Count
        {
            get
            {
                return _DbParameterInfoList.Count;
            }
        }

        /// <summary>
        /// 配合foreach使用,必须有GetEnumerator()方法
        /// </summary>
        /// <returns></returns>
        public DbKeyValueList GetEnumerator()
        {
            Reset();
            return this;
        }

        /// <summary>
        /// 获取当前项
        /// </summary>
        public DbParameterInfo Current
        {
            get
            {
                try
                {
                    return _DbParameterInfoList[_Position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            _Position++;
            return (_Position < _DbParameterInfoList.Count);
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            _Position = -1;
        }



    }
}
